<?php
namespace Modules\Cms;
require_once dirname(__FILE__) .'/../common.php';

class Link
{
    const TABLE = 't_cms_link';

    public static function fetch($space, $idx = '', $limit = 20, $page = 1, $all = false)
    {
        $where = '';
        $idx && $where .= ' AND idx="'. $idx .'"';
        $limit_str = ' LIMIT '. ($page - 1) * $limit .','. $limit;
        $select = $all ? '*' : 'id, space, idx, title, subtitle, link, img, thumb, tm';
        $sql = 'SELECT SQL_CALC_FOUND_ROWS '. $select .' FROM '. self::TABLE .' WHERE space="'. $space .'" AND status=1'. $where .' ORDER BY rank, sort DESC'. $limit_str;
        return getDb()->fetchAll($sql);
    }
    
    public static function search($keyword, $space, $idx = '', $limit = 20, $page = 1)
    {
        $where = '';
        $idx && $where .= ' AND idx="'. $idx .'"';
        $limit_str = ' LIMIT '. ($page - 1) * $limit .','. $limit;
        $sql = 'SELECT SQL_CALC_FOUND_ROWS id, space, idx, title, subtitle, link, img, thumb, tm FROM '. self::TABLE .' WHERE title like "%'. $keyword .'%" AND space="'. $space .'" AND status=1'. $where .' ORDER BY rank, sort DESC'. $limit_str;
        return getDb()->fetchAll($sql);
    }


    public static function fetchContent($space, $idx = '', $limit = 1)
    {
        $where = '';
        $idx && $where .= ' AND idx="'. $idx .'"';
        $limit_str = $limit ? ' LIMIT '. $limit : '';
        $sql = 'SELECT * FROM '. self::TABLE .' WHERE space="'. $space .'" AND status=1'. $where .' ORDER BY rank, sort DESC'. $limit_str;
        if ($limit == 1) {
            return getDb()->fetchRow($sql);
        } else {
            $data = getDb()->fetchAll($sql);
            if (!$idx) {
                $result = array();
                foreach ($data as $row) {
                    $result[$row['idx']][] = $row;
                }
                return $result;
            } else {
                return $data;
            }
        }
    }
    
    public static function fetchOne($id)
    {
        $sql = 'SELECT * FROM '. self::TABLE .' WHERE id='. $id;
        return getDb()->fetchRow($sql);
    }
    
    public static function fetchNear($idx, $sort)
    {
        $sql = 'SELECT id, title FROM '. self::TABLE .' WHERE idx="'. $idx .'" AND sort>"'. $sort .'" ORDER BY sort ASC LIMIT 1';
        $next = getDb()->fetchRow($sql);
        $sql = 'SELECT id, title FROM '. self::TABLE .' WHERE idx="'. $idx .'" AND sort<"'. $sort .'" ORDER BY sort DESC LIMIT 1';
        $pre = getDb()->fetchRow($sql);
        return array('pre'=>$pre, 'next'=>$next);
    }


    protected static function getCount()
    {
        static $count = null;
        if (null === $count) {
            $count = getDb()->fetchOne('SELECT FOUND_ROWS()');
        }
        return $count;
    }

    public static function getPages($page = 1, $perpage = 20, $type = 'html')
    {
        $count = self::getCount();
        $page_show = '';
        if ($count > $perpage) {
            $PG = new \SF\Widgets\Paginator($page, $count, $perpage);
            $page_show = ($type == 'html') ? $PG->getRender() : $PG->getPages();
        }
        return $page_show;
    }
    
}
